package cn.lg.soar.message.entity;

import cn.lg.soar.common.util.data.StringUtil;
import cn.lg.soar.message.api.enums.MailSendStatusEnum;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * @author luguoxiang
 * @date 2022/4/5
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ApiModel(value = "邮件发送日志", description = "邮件发送日志")
@Getter
@Setter
@TableName("lg_msg_mail_log")
public class MailLog implements Serializable {

    @ApiModelProperty(value = "主键id")
    @TableId
    private Long id;

    @ApiModelProperty(value = "主题")
    private String subject;

    @ApiModelProperty(value = "内容")
    private String content;

    @ApiModelProperty(value = "接收人")
    @TableField(typeHandler = Handler.class)
    private String[] tos;

    private LocalDateTime time;

    private MailSendStatusEnum status;

    public static class Handler extends BaseTypeHandler<String[]> {

        @Override
        public void setNonNullParameter(PreparedStatement preparedStatement, int i, String[] strings, JdbcType jdbcType) throws SQLException {
            if (strings != null) {
                preparedStatement.setString(i, String.join(",", strings));
            }
        }

        @Override
        public String[] getNullableResult(ResultSet resultSet, String colName) throws SQLException {
            String string = resultSet.getString(colName);
            if (string == null) {
                return StringUtil.EMPTY_ARRAY;
            }
            return string.split(",");
        }

        @Override
        public String[] getNullableResult(ResultSet resultSet, int i) throws SQLException {
            String string = resultSet.getString(i);
            if (string == null) {
                return StringUtil.EMPTY_ARRAY;
            }
            return string.split(",");
        }

        @Override
        public String[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
            String string = callableStatement.getString(i);
            if (string == null) {
                return StringUtil.EMPTY_ARRAY;
            }
            return string.split(",");
        }

    }
}
